/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  com.alibaba.fastjson.JSON
 *  com.alibaba.fastjson.JSONObject
 *  de.felixroske.jfxsupport.FXMLController
 *  de.felixroske.jfxsupport.GUIState
 *  javafx.application.Platform
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.PasswordField
 *  javafx.scene.control.TextField
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.GridPane
 *  javafx.stage.Modality
 *  javafx.stage.Stage
 *  org.java_websocket.client.WebSocketClient
 *  org.java_websocket.drafts.Draft
 *  org.java_websocket.drafts.Draft_6455
 *  org.java_websocket.handshake.ServerHandshake
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.util.StringUtils
 */
package com.gys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gys.GysApplication;
import com.gys.common.business.GysUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.ConstUtil;
import com.gys.controller.BaseController;
import com.gys.controller.OTO.RemindTipController;
import com.gys.controller.OfflineController;
import com.gys.controller.PhoneLoginController;
import com.gys.controller.StoreController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.dataManger.TokenManger;
import com.gys.data.O2O.GetElemeOutData;
import com.gys.data.example.GetUserOutData;
import com.gys.view.O2O.RemindTipView;
import com.gys.view.PhoneLoginView;
import com.gys.view.StoreView;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.net.URI;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

@FXMLController
public class LoginController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);
    @FXML
    protected TextField account;
    @FXML
    private PasswordField password;
    @FXML
    private GridPane loginGridPane;
    @Autowired
    private StoreController storeController;
    @Autowired
    private PhoneLoginController phoneLoginController;
    @Autowired
    private RemindTipController remindTipController;
    @Autowired
    private OfflineController offlineController;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.clear();
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        Stage stage = GUIState.getStage();
        stage.setResizable(false);
        GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle maximumWindowBounds = graphicsEnvironment.getMaximumWindowBounds();
        Double height = maximumWindowBounds.getHeight();
        Double width = maximumWindowBounds.getWidth();
        stage.setMinHeight(height.doubleValue());
        stage.setMinWidth(width.doubleValue());
        this.init();
    }

    @Override
    public void init() {
        this.offlineController.isConnectNoData();
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                GUIState.getStage().getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.ENTER) {
                            LoginController.this.handleLoginAction();
                        }
                        e.consume();
                    }
                });
            }
        }, 500L);
    }

    @FXML
    private void handleLoginAction() {
        String accountStr = this.account.getText().trim();
        String passwordStr = this.password.getText().trim();
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        if (StringUtils.isEmpty(accountStr) || StringUtils.isEmpty(passwordStr)) {
            alert.setContentText("\u8d26\u53f7\u6216\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a");
            alert.showAndWait();
            return;
        }
        HttpJson httpJson = GysUtil.loginByAcoount(accountStr, passwordStr);
        if (httpJson.isSuccess()) {
            GetUserOutData loginUser = (GetUserOutData)JSON.parseObject((String)httpJson.getMsg(), GetUserOutData.class);
            loginUser.setAccount(loginUser.getUserId());
            String token = loginUser.getToken();
            TokenManger.setToken(token);
            CacheUtil.put(token, loginUser);
            CacheUtil.put("storeList", loginUser.getStoreList());
            CacheUtil.put("logInUser", loginUser);
            try {
                WebSocketClient webSocketClient = new WebSocketClient(new URI(ConstUtil.SOCKET_URL + ((GetUserOutData)CacheUtil.get("logInUser")).getUserId()), (Draft)new Draft_6455()){

                    public void onOpen(ServerHandshake handshakedata) {
                        log.info("[websocket] \u8fde\u63a5\u6210\u529f");
                    }

                    public void onMessage(String message) {
                        log.info("[websocket] \u6536\u5230\u6d88\u606f={}", message);
                        GetElemeOutData elemeOutData = (GetElemeOutData)JSONObject.parseObject((String)message, GetElemeOutData.class);
                        if ("orderCreate".equals(elemeOutData.getType())) {
                            Platform.runLater((Runnable)new Runnable(){

                                @Override
                                public void run() {
                                    LoginController.this.remindTipController.init();
                                    CommonUtil.showView(RemindTipView.class, Modality.APPLICATION_MODAL);
                                }
                            });
                        }
                    }

                    public void onClose(int code, String reason, boolean remote) {
                        log.info("[websocket] \u9000\u51fa\u8fde\u63a5");
                    }

                    public void onError(Exception ex) {
                        log.info("[websocket] \u8fde\u63a5\u9519\u8bef={}", ex.getMessage());
                    }
                };
                webSocketClient.connect();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            this.storeController.initStoreList();
            GysApplication.showView(StoreView.class, (Modality)Modality.APPLICATION_MODAL);
        } else {
            alert.setContentText(httpJson.getMsg());
            alert.showAndWait();
        }
    }

    @FXML
    private void phoneLoginAction() {
        this.phoneLoginController.init();
        GysApplication.showView(PhoneLoginView.class);
    }
}

